const koa = require("koa"); // 不能用esmodule import koa from "koa";

const koaApp = new koa();
koaApp.listen(8888, () => {
  console.log("koa started at 8888")
})

function delay(duration) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve()
    }, duration)
  })
}

koaApp.use(async (ctx) => {
  console.log("ctx", ctx);
  const {path} = ctx
  if (path === "/student") { // 代表有人如果请求我们的/student接口 我们就要干点什么
    console.log("student matched")
    // 返回数据
    const jsonData = require("./student.json")
    console.log(jsonData)
    await delay(2000);
    //返回客户端
    ctx.response.body = jsonData
  }
})